package com.qingzhu.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.qingzhu.pojo.Goods;
import com.qingzhu.pojo.Item;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/* 封装接收的参数到实体类中 */
public class ParamsToObject {
    private final static Goods GOODS = new Goods();
    private final static Item ITEM = new Item();

    // 将参数封装到SPU实体
    public static Goods getGoods(HttpServletRequest req) {
        GOODS.setGoodsName(req.getParameter("goodsName"));
        GOODS.setTitle(req.getParameter("title"));
        String brandId = req.getParameter("brand");
        GOODS.setBrandId(Integer.parseInt(brandId));
        String price = req.getParameter("price");
        GOODS.setPrice(Double.parseDouble(price));
        // 解析SPU规格参数
        List<String> list = new ArrayList<>();
        String qzsxt = req.getParameter("qzsxt");
        list.add(qzsxt);
        String hzsxt = req.getParameter("hzsxt");
        list.add(hzsxt);
        String spzl = req.getParameter("spzl");
        list.add(spzl);
        String zpcc = req.getParameter("zpcc");
        list.add(zpcc);
        String cpu = req.getParameter("cpu");
        list.add(cpu);
        String xtnh = req.getParameter("xtnh");
        list.add(xtnh);
        String sim = req.getParameter("sim");
        list.add(sim);
        String dcrl = req.getParameter("dcrl");
        list.add(dcrl);
        ListTypeHandler listTypeHandler = new ListTypeHandler();
        String string = listTypeHandler.listToString(list);
        GOODS.setAttributeList(string);
        return GOODS;
    }
    // 将参数封装到SKU实体
    public static Item getItem(HttpServletRequest req) {
        String itemPrice = req.getParameter("itemPrice");
        ITEM.setPrice(Double.parseDouble(itemPrice));
        String itemNum = req.getParameter("itemNum");
        ITEM.setNum(Integer.parseInt(itemNum));
        ITEM.setImage(req.getParameter("addimg"));
        DateTime date = DateUtil.date();
        ITEM.setCreateTime(date);
        ITEM.setColour(req.getParameter("colour"));
        ITEM.setNetwork(req.getParameter("network"));
        String yxnc = req.getParameter("yxnc");
        String jsnc = req.getParameter("jsnc");
        ITEM.setEdition(yxnc+"/"+jsnc);
        return ITEM;
    }
}
